feat: add Uint8Array support for zero-copy worker_threads transfer#3183
Open
umeshmore45 wants to merge 12 commits intoredis:masterfrom
Open
feat: add Uint8Array support for zero-copy worker_threads transfer#3183umeshmore45 wants to merge 12 commits intoredis:masterfrom
umeshmore45 wants to merge 12 commits intoredis:masterfrom
Conversation
- Add Uint8Array to type system (BlobStringReply, SimpleStringReply, VerbatimStringReply) - Implement Uint8Array encoding/decoding in RESP protocol handlers - Support Uint8Array as command arguments across all commands - Add comprehensive test coverage for Uint8Array operations - Fix type guards in HSET, GEOSEARCH, AGGREGATE, SEARCH commands - Fix Buffer/Uint8Array handling in time-series helpers and cluster-slots - Maintain full backward compatibility with Buffer Enables zero-copy transfer to worker_threads via ArrayBuffer transfer. Closes redis#3106
|
Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset. In case there are security findings, they will be communicated to you as a comment inside the PR. Hope you’ll enjoy using Jit. Questions? Comments? Want to learn more? Get in touch with us. |
Collaborator
|
Thanks @umeshmore45, i will take a look |
nkaradzhov
requested changes
Mar 2, 2026
Collaborator
There was a problem hiding this comment.
Please address the below:
Uint8Arraykeys are misclassified in sorted-set key parsing (ZINTER/ZUNION/ZUNIONSTORE).
Add parseArgs regression cases inZINTER.spec.ts,ZUNION.spec.ts, andZUNIONSTORE.spec.tsfor single and arrayUint8Arraykeys.CONFIG SETsingle-parameter form treatsUint8Arrayas object entries.
Add regression inCONFIG_SET.spec.tsverifying single-parameterUint8Arrayinput uses the correct branch.CMS.MERGEhas the same plain-argument discriminator bug forUint8Array[].
Add regression inMERGE.spec.tsfor plainUint8Array[]sources (no weights).- Client-side cache invalidation key normalization is inconsistent for
Uint8ArrayvsBuffer/string.
Add regression incache.spec.tsproving a cachedUint8Arraykey is invalidated by equivalent server-style invalidation payloads. - Tuple/map key transforms use
.toString()onUint8Array, producing byte-list keys ("102,111,111").
Add regressions inHGETALL.spec.tsandPUBSUB_NUMSUB.spec.tsasserting semantic string keys are preserved underUint8Arraymapping. - Numeric transforms parse
Uint8Arrayreplies asNaN.
Add command-level regressions inGEODIST.spec.ts,ZRANK_WITHSCORE.spec.ts, andGET.spec.ts. RESEThandling in queue does not recognizeUint8ArraySIMPLE_STRINGreplies and can hang in monitor/type-mapped flows.
Add a new queue unit test (underpackages/client/lib/client, e.g.commands-queue.spec.ts) covering monitor withSIMPLE_STRING->Uint8Arrayand ensuringreset()resolves.RESP2pub/subPINGresolution path ignoresUint8Arraymapping behavior.
Add queue-level regression test provingRESP2pub/subPINGrespectsSIMPLE_STRINGmapping when mapped toUint8Array.- Test strictness must be tightened so type regressions are detectable.
Replace permissivedeepEqualwithdeepStrictEqualwhere binary type identity matters indecoder.spec.ts:66.
For all newUint8Arrayassertions, require bothinstanceof Uint8Arrayand!Buffer.isBuffer(value)(includingtypes.spec.ts:54).
For each item, include both the code fix and a regression test that would fail without the fix and pass with it.
…t8Array as valid key types - Updated type guard in generic-transformers to recognize Uint8Array. - Added unit tests for Uint8Array support in ZINTER, ZUNION, and ZUNIONSTORE commands. - Ensured compatibility with existing command structures while expanding argument handling. This change improves the flexibility of command arguments, allowing for zero-copy transfers with Uint8Array.
- Updated type guard to recognize Uint8Array as a valid parameter type. - Added a regression test to ensure correct handling of Uint8Array keys. - Improved argument parsing to prevent incorrect behavior with Uint8Array inputs. This change enhances the flexibility of the CONFIG SET command, aligning with recent updates for better Uint8Array support across commands.
- Updated type guard in isPlainSketches to include Uint8Array. - Added regression test to verify correct handling of Uint8Array sources in CMS.MERGE. This change improves the argument handling of the CMS.MERGE command, ensuring compatibility with Uint8Array inputs.
- Introduced tests to ensure proper invalidation of cache entries when using Uint8Array keys. - Validated that entries are evicted correctly when invalidation keys are provided as Buffer or string. - Enhanced the client-side cache functionality to handle Uint8Array keys consistently with server-side invalidation. This change improves the reliability of cache behavior in scenarios involving Uint8Array keys.
… commands - Introduced regression tests to ensure Uint8Array field keys and channel names are correctly decoded as UTF-8 strings. - Validated that the transformation of replies maintains the integrity of keys when using Uint8Array. This change enhances the reliability of key handling in the HGETALL and PUBSUB NUMSUB commands, aligning with recent updates for better Uint8Array support.
…, GET, and ZRANK_WITHSCORE commands - Introduced tests to validate that Uint8Array replies are correctly parsed as finite numbers instead of NaN. - Ensured that the transformation of replies handles Uint8Array correctly across multiple commands, addressing a coercion hazard. This change enhances the reliability of numeric handling in commands that utilize Uint8Array, improving overall data integrity.
- Refactored the handling of the reply to use a more robust check for Uint8Array. - Enhanced the condition to ensure accurate detection of replies, improving the reliability of the command queue's response handling. This change addresses potential issues with reply processing in PubSub and MONITOR modes.
…ray and Buffer - Added tests to ensure the PING command resolves correctly as Uint8Array, Buffer, or string based on the provided typeMapping. - Improved the handling of PONG replies to accurately return the expected types, enhancing the command queue's response reliability. This change addresses potential inconsistencies in the response types for the PING command, aligning with recent updates for better Uint8Array support.
- Changed to in decoder tests to ensure strict equality checks. - Enhanced type checks in types tests to clarify expectations for Uint8Array instances and prevent Buffer confusion. These changes improve the reliability of test assertions, ensuring more accurate validation of expected outcomes.
…rray and Buffer - Introduced a new utility function to standardize the conversion of Redis keys from string, Buffer, or Uint8Array to UTF-8 strings. - Updated cache management in to utilize the new key conversion function, ensuring consistent key handling. - Added function to convert Redis blob replies to UTF-8 strings, improving the handling of Uint8Array and Buffer replies across multiple commands. - Refactored commands such as GEODIST, PUBSUB_NUMSUB, and ZRANK_WITHSCORE to leverage for accurate reply transformations. These changes enhance the reliability and consistency of key and reply handling, particularly for Uint8Array and Buffer types, aligning with recent updates for better data integrity.
…ecoder and encoder - Cleaned up the code by removing unnecessary comments that indicated Uint8Array support in the decoder and encoder files. - This change improves code readability and maintains focus on the relevant logic without cluttering with repetitive comments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR adds comprehensive
Uint8Arraysupport to enable zero-copy data transfer to worker_threads, significantly improving performance for multi-threaded applications handling large payloads.Motivation
Currently, Redis values are returned as
Buffer, which requires copying when transferring to worker threads. WithUint8Arraysupport, the underlyingArrayBuffercan be transferred with zero-copy semantics.Performance Benefits:
For a 10MB payload, this eliminates 10MB of memory copying when transferring between threads.
Usage Example
Changes Made
Type System (
types.ts):BlobStringReply,SimpleStringReply,VerbatimStringReplyto supportUint8ArrayUnwrapConstructorandReplyWithTypeMappingto handleUint8ArrayUint8ArraytoRedisArgumenttypeDecoder (
decoder.ts):Encoder (
encoder.ts):Command Support:
HSET,GEOSEARCH,SEARCH,AGGREGATEto accept Uint8Array argumentscluster-slots.tsandtime-series/helpers.tsTesting:
Breaking Changes
None! This is fully backward compatible:
Bufferremains the default typeUint8Arrayis opt-in viawithTypeMapping()Files Changed
Closes #3106
Checklist
npm testpass with this change (including linting)?Note: Documentation can be added in a follow-up PR if needed. The code includes inline comments and the PR description provides usage examples.
Note
Medium Risk
Touches core request/response encoding/decoding, reply typing, and queue/caching invalidation paths; regressions could affect many commands and pub/sub/monitor behavior. Changes are largely additive and backed by targeted regression tests for Uint8Array mappings.
Overview
Adds first-class
Uint8Arraysupport end-to-end:RedisArgumentand reply types now includeUint8Array, and the RESPdecoder/encodercan emit/accept plainUint8Arraywhen configured viatypeMapping.Fixes a set of
Uint8Arrayregressions caused byBuffer-only guards andUint8Array#toString()semantics, including numeric parsing (GEODIST,ZRANK WITHSCORE), tuple/map key decoding (e.g.HGETALL,PUBSUB NUMSUB), cluster slot calculation, and RESP2 pub/sub/monitor/reset handling incommands-queue.Updates client-side cache invalidation to normalize Redis keys consistently across
string/Buffer/Uint8Arrayand adds comprehensive tests covering encoding/decoding, type-mapping, command argument parsing, and cache/queue behaviors.Written by Cursor Bugbot for commit 64936ec. This will update automatically on new commits. Configure here.